package com.yjt.fresh.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiUpdateUserInfo;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanUserInfo;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToastResult;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 昵称
 * Created by mcc on 2018-07-18.
 */

public class AtyNickName extends BaseSimpleActivity {
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.edtAppAtyNickName)
    EditText mEdt;
    @BindView(R.id.btnAppAtyNickNameSubmit)
    Button mBtnSubmit;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_nickname;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        mBtnSubmit.setClickable(false);
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0){
                    mBtnSubmit.setClickable(true);
                    mBtnSubmit.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                }else {
                    mBtnSubmit.setClickable(false);
                    mBtnSubmit.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateNickName();
            }
        });
    }

    HttpOnNextListener<BaseResultEntity> mListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.show("修改成功");
            BeanUserInfo userInfo = PreferenceManager.getInstance().getUserInfo();
            userInfo.setNICKNAME(mEdt.getText().toString());
            PreferenceManager.getInstance().setUserInfo(userInfo);
            finish();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void updateNickName() {
        ApiUpdateUserInfo apiUpdateUserInfo = new ApiUpdateUserInfo(mListener,mContext,true,false,true);
        Map<String,Object> map = new HashMap<>();
        map.put("NICKNAME",mEdt.getText().toString());
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiUpdateUserInfo.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiUpdateUserInfo);
    }

}
